package pkg

import (
	"fmt"
	"strings"

	termbox "github.com/nsf/termbox-go"
)

type Basic struct {
	StartX  int               // 左上角X起点位置
	StartY  int               // 左上角Y起点位置
	LengthX int               // 矩形长
	LengthY int               // 矩形宽
	Fg      termbox.Attribute // 字体背景颜色
	Bg      termbox.Attribute // 背景颜色
}

func (this *Basic) FillLine() *Basic {
	// 横着
	for i := 1; i < this.LengthX; i++ {
		termbox.SetCell(this.StartX+i, this.StartY, '─', this.Fg, this.Bg)
		termbox.SetCell(this.StartX+i, this.StartY+this.LengthY, '─', this.Fg, this.Bg)
	}
	// 竖着
	for i := 1; i < this.LengthY; i++ {
		termbox.SetCell(this.StartX, this.StartY+i, '│', this.Fg, this.Bg)
		termbox.SetCell(this.StartX+this.LengthX, this.StartY+i, '│', this.Fg, this.Bg)
	}
	return this
}

func (this *Basic) Border() *Basic {
	// 画边框
	termbox.SetCell(this.StartX, this.StartY, '┌', this.Fg, this.Bg)
	termbox.SetCell(this.StartX+this.LengthX, this.StartY, '┐', this.Fg, this.Bg)
	termbox.SetCell(this.StartX, this.StartY+this.LengthY, '└', this.Fg, this.Bg)
	termbox.SetCell(this.StartX+this.LengthX, this.StartY+this.LengthY, '┘', this.Fg, this.Bg)
	this.FillLine()
	return this
}

func (this *Basic) SetFg(color termbox.Attribute) *Basic {
	this.Fg = color
	return this
}

func (this *Basic) SetBg(color termbox.Attribute) *Basic {
	this.Bg = color
	return this
}

func (this *Basic) SetStartX(data int) *Basic {
	this.StartX = data
	return this
}

func (this *Basic) SetStartY(data int) *Basic {
	this.StartY = data
	return this
}

func (this *Basic) SetLengthX(data int) *Basic {
	this.LengthX = data
	return this
}

func (this *Basic) SetLengthY(data int) *Basic {
	this.LengthY = data
	return this
}

// 横着写
func (this *Basic) SetValueH(msg string) *Basic {
	if len(msg) > this.LengthX {
		tmp := strings.Split(msg, "")
		if this.LengthX <= 3 {
			msg = "..."
		} else {
			msg = fmt.Sprintf("%s...", strings.Join(tmp[:this.LengthX-3], ""))
		}
	}
	Hprint(this.StartX+1, this.StartY+1, this.Fg, this.Bg, msg)
	return this
}

// 竖着写
func (this *Basic) SetValueS(msg string) *Basic {
	if len(msg) > this.LengthY {
		tmp := strings.Split(msg, "")
		if this.LengthY <= 3 {
			msg = "..."
		} else {
			msg = fmt.Sprintf("%s...", strings.Join(tmp[:this.LengthY-3], ""))
		}
	}
	Sprint(this.StartX+1, this.StartY+1, this.Fg, this.Bg, msg)
	return this
}

// 计算可用内容
